package com.example.demotest.demos.bens;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jcraft.jsch.JSchException;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CommandControl {
    public static void startTranslate() throws JSchException, IOException {
        // 创建一个空的Word文档
        XWPFDocument document = new XWPFDocument();
        List<SSHClient> sshClientList = FileInput.getFileSshClientList();
        for (SSHClient sshClient : sshClientList) {
            // 创建一个段落
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            //居中
            paragraph.setAlignment(ParagraphAlignment.CENTER);
            // 设置文本
            run.setText("开始巡检主机" + sshClient.getHost());
            run.setColor("0000FF");
            run.setFontSize(14);
            //粗体
            run.setBold(true);
            //字体样式
            run.setFontFamily("Arial");

            System.out.println("开始访问主机" + sshClient.getHost() + "，打开链接");
            sshClient.login();

            //厂商及型号
            XWPFParagraph paragManufacturer = document.createParagraph();
            XWPFRun runManu = paragManufacturer.createRun();
            JSONObject manufacturerInfo = SshCommand.getManufacturerInfo(sshClient);
            JSONArray manufacturerArray = null;
            if(manufacturerInfo.containsKey("manufacturer")){
                 manufacturerArray = manufacturerInfo.getJSONArray("manufacturer");
            }
            runManu.setText("厂商信息和型号如下：");
            runManu.setColor("0000FF");
            runManu.setBold(true);
            runManu.setText("\r");
            runManu = paragManufacturer.createRun();
            if(manufacturerArray!= null && manufacturerArray.size() > 0){
                for (int i = 0; i < manufacturerArray.size(); i++) {
                    runManu.setText(manufacturerArray.getString(i));
                    runManu.setText("\r");
                }
            }

            //操作系统类型和版本
            XWPFParagraph paragSystem = document.createParagraph();
            XWPFRun runSys = paragSystem.createRun();
            JSONObject systemInfo = SshCommand.getSystemInfo(sshClient);
            String systemUsageRate = "";
            if(systemInfo.containsKey("msg")){
                systemUsageRate = systemInfo.getString("msg");
            }
            runSys.setText("主机操作系统类型和版本如下：" + systemUsageRate + "; \r");

            //获取磁盘信息
            // 创建一个磁盘信息段落
            XWPFParagraph paragraph2 = document.createParagraph();
            double exceedUsageRateL = 80;
            JSONObject diskInfo = SshCommand.getDiskInfo(sshClient, exceedUsageRateL);
            JSONArray usageRate = diskInfo.getJSONArray("usageRate");
            XWPFRun run2 = paragraph2.createRun();
            run2.setText("巡检磁盘信息如下：");
            run2.setColor("0000FF");
            run2.setBold(true);
            run2.setText("\r");
            run2 = paragraph2.createRun();
            for (int i = 0; i < usageRate.size(); i++) {
                run2.setText(usageRate.getString(i));
                run2.setText("\r");
            }
            JSONArray exceedUsageRate = diskInfo.getJSONArray("exceedUsageRate");
            run2 = paragraph2.createRun();
            run2.setText("巡检磁盘超过" + exceedUsageRateL + "阈值如下：");
            run2.setColor("0000FF");
            run2.setBold(true);
            run2.setText("\r");
            run2 = paragraph2.createRun();
            if (exceedUsageRate.size() <= 0) {
                run2.setText("服务器整体磁盘使用率未存在超过" + exceedUsageRateL + "%，请放心使用");
                run2.setText("\r");
            } else {
                for (int i = 0; i < exceedUsageRate.size(); i++) {
                    run2.setText(exceedUsageRate.getString(i));
                    run2.setColor("FF0000");
                    run2.setText("\r");
                }
            }


            //获取内存使用率
            // 创建一个内存使用率段落
            XWPFParagraph paragraph3 = document.createParagraph();
            XWPFRun run3 = paragraph3.createRun();
            exceedUsageRateL = 80;
            JSONObject memoryInfo = SshCommand.getMemoryInfo(sshClient, exceedUsageRateL);
            JSONArray memoryUsageRate = memoryInfo.getJSONArray("usageRate");
            run3.setText("内存使用率信息如下（G）：");
            run3.setColor("0000FF");
            run3.setBold(true);
            run3.setText("\r");
            run3 = paragraph3.createRun();
            for (int i = 0; i < memoryUsageRate.size(); i++) {
                run3.setText(memoryUsageRate.getString(i));
                run3.setText("\r");
            }

            JSONArray memoryExceedUsageRate = memoryInfo.getJSONArray("exceedUsageRate");
            run3 = paragraph3.createRun();
            run3.setText("内存使用率超过" + exceedUsageRateL + "阈值如下：");
            run3.setColor("0000FF");
            run3.setBold(true);
            run3.setText("\r");
            run3 = paragraph3.createRun();
            if (memoryExceedUsageRate.size() <= 0) {
                run3.setText("服务器整体内存使用率未存在超过" + exceedUsageRateL + "%，请放心使用");
            } else {
                for (int i = 0; i < memoryExceedUsageRate.size(); i++) {
                    run3.setText(memoryExceedUsageRate.getString(i));
                    run3.setColor("FF0000");
                    run3.setText("\r");
                }
            }

            //CPU使用率
            // 创建一个CPU使用率段落
            XWPFParagraph paragraph4 = document.createParagraph();
            XWPFRun run4 = paragraph4.createRun();
            exceedUsageRateL = 80;
            JSONObject cpuInfo = SshCommand.getCpuInfo(sshClient, exceedUsageRateL);
            JSONArray cpuUsageRate = cpuInfo.getJSONArray("usageRate");
            run4.setText("CPU使用率巡检信息如下：");
            run4.setColor("0000FF");
            run4.setBold(true);
            run4.setText("\r");
            run4 = paragraph4.createRun();
            for (int i = 0; i < cpuUsageRate.size(); i++) {
                run4.setText(cpuUsageRate.getString(i));
                run4.setText("\r");
            }
            JSONArray cpuExceedUsageRate = cpuInfo.getJSONArray("exceedUsageRate");
            run4 = paragraph4.createRun();
            run4.setText("CPU使用率超过" + exceedUsageRateL + "阈值如下：");
            run4.setColor("0000FF");
            run4.setBold(true);
            run4.setText("\r");
            run4 = paragraph4.createRun();
            if (cpuExceedUsageRate.size() <= 0) {
                run4.setText("服务器整体CPU使用率未存在超过" + exceedUsageRateL + "%，请放心使用");
            } else {
                for (int i = 0; i < cpuExceedUsageRate.size(); i++) {
                    run4.setText(cpuExceedUsageRate.getString(i));
                    run4.setColor("FF0000");
                    run4.setText("\r");
                }
            }

            System.out.println("结束主机访问" + sshClient.getHost() + "，关闭链接");
            sshClient.logout();
        }
        // 将文档写入到磁盘上的文件中
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        String myDate = simpleDateFormat.format(new Date());
        FileOutputStream out = new FileOutputStream(myDate+".docx");
        document.write(out);
        out.close();
        System.out.println("Word文档已创建！");
    }
}
